_.templateSettings = {
  interpolate : /\{\{(.+?)\}\}/g
};

var LoadMore = function(options){
	var _self = this;

	var defaults ={
		template:'',
		el:'', 
		btn:'',
		url:'',
		data:{},
		pagecount:0,
		pagesize:0,
		pageindex:1,
		lazyload:false,
		lazyHight:0,
	};
	$.extend(defaults,options);

	_self.isloadding= false;

	_self.initialize= function(){
		_self.template= _.template($('#'+defaults.template).html());
		_self.el = $("#"+defaults.el);
		_self.btn = $('#'+defaults.btn);
		_self.togglebtn();
		_self.bind();
	}

	_self.page = function(){
		return parseInt(defaults.pagecount/defaults.pagesize) + (defaults.pagecount%defaults.pagesize>0?1:0);	
	}
	

	_self.bind = function(){
		if(defaults.lazyload &&_self.page()>0)
		{
			_self.btn.text("加载中.......").show();
			var winH = $(window).height(); //页面可视区域高度 
			if($(document.body).height()>winH){
				$(window).scroll(function () { 
			        var pageH = $(document.body).height(); 
			        var scrollT = $(window).scrollTop(); //滚动条top 
			        if(scrollT+winH+defaults.lazyHight>= _self.btn.offset().top){ 
			            if(!_self.isloadding &&  defaults.pageindex<=_self.page() ) _self.ajax();
			        } 
		    	}); 
			}
			else{
				if(!_self.isloadding &&  defaults.pageindex<=_self.page() ) _self.ajax();
			}
		    
		}
		else
		{
			_self.btn.click(function(){
				_self.btn.text("加载中.......");
				_self.ajax();
			});
		}
		
	}

	_self.ajax= function(){
		_self.isloadding=true;
		$.getJSON(defaults.url,$.extend({ajax:1,page:defaults.pageindex,_:new Date().getTime()},defaults.data), function(rs){
					_self.render(rs.result);
					_self.btn.text("加载更多>>");
					defaults.pageindex++;
					_self.togglebtn();
					_self.isloadding=false;
		},"json");
	}

	_self.togglebtn = function(){
		if (defaults.pageindex>_self.page()) {
			_self.btn.hide();
		}
		else{
			_self.btn.show();
		}
	}


	_self.render = function(items){
		var html=[];
		_.each(items,function(item){
			html.push(_self.template(item));
		});
		_self.el.append(html.join(''));
	}
	_self.initialize();
}

Date.prototype.format =function(format)
{
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(), //day
	"h+" : this.getHours(), //hour
	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
	"q+" : Math.floor((this.getMonth()+3)/3), //quarter
	"S" : this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
	(this.getFullYear()+"").substr(4- RegExp.$1.length));
	for(var k in o)if(new RegExp("("+ k +")").test(format))
	format = format.replace(RegExp.$1,
	RegExp.$1.length==1? o[k] :
	("00"+ o[k]).substr((""+ o[k]).length));
	return format;
}

function jsDateDiff(publishTime){       
    //dateSeperate = "-"       
    var d_minutes,d_hours,d_days;       
    var timeNow = parseInt(new Date().getTime()/1000);       
    var d;       
    d = timeNow - publishTime;       
    d_days = parseInt(d/86400);       
    d_hours = parseInt(d/3600);       
    d_minutes = parseInt(d/60);       
    if(d_days>0 && d_days<4){       
        return d_days+"天前";       
    }else if(d_days<=0 && d_hours>0){       
        return d_hours+"小时前";       
    }else if(d_hours<=0 && d_minutes>0){       
        return d_minutes+"分钟前";       
    }else{       
        var s = new Date(publishTime*1000);       
        return s.format("yyyy-MM-dd hh:mm:ss");       
    }       
}       
